﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

using FbKPI.Model;
namespace FbKPI.Controller
{
    public class LocationController : FbKPI.Controller.dbConnection.ConnectionClass
    {
        public string Save(Location locations)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "UP_InsertUpdateLocation";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = locations.ID;
            command.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = locations.Description;
            command.Parameters.Add("@ISACTIVE", SqlDbType.VarChar).Value = locations.isActive;
            command.Parameters.Add("@ISHEADOFFICE", SqlDbType.VarChar).Value = locations.isHeadOffice;
            command.Parameters.Add("@RGMID", SqlDbType.VarChar).Value = locations.RGM.ID;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = locations.LastModifiedBy;
            command.Parameters.Add("@CREATEBY", SqlDbType.VarChar).Value = locations.CreateBy;
            command.Parameters.Add("@ORGID", SqlDbType.VarChar).Value = locations.OrgID;
            command.Parameters.Add("@BusinessGroupID", SqlDbType.VarChar).Value = locations.BusinessGroupID;

            return ExecuteNonQuery(command);
        }

        public string Delete(Location locations)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "UP_DeleteLocation";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = locations.ID;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = locations.LastModifiedBy;

            return ExecuteNonQuery(command);
        }

        public string BeforeDelete(Location locations)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_CheckBeforeDetleteLocation";
            command.Parameters.Add("@ID", SqlDbType.Int).Value = locations.ID;
            

            return GetSingleData(command);
        }

        public IList<Location> GetAll(string sSearch, int PageNumber, int Datalength, string SortCol, string SortDirection, out int TotalData, int BusinessGroupID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetAllLocations";
            command.Parameters.Add("@SEARCH", SqlDbType.VarChar).Value = sSearch;
            command.Parameters.Add("@BusinessGroupID", SqlDbType.VarChar).Value = BusinessGroupID;

            DataSet dx = GetTable(command);

            List<Location> Locations = new List<Location>();

            switch (SortDirection)
            {
                case "asc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description ASC";
                            break;
                        case "RGM":
                            dx.Tables[0].DefaultView.Sort = "RGMID ASC";
                            break;
                        case "ORGID":
                            dx.Tables[0].DefaultView.Sort = "ORGID ASC";
                            break;
                        case "LastModifiedDate":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedDate ASC";
                            break;
                        case "LastModifiedBy":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedBy ASC";
                            break;
                        case "CreateDate":
                            dx.Tables[0].DefaultView.Sort = "CreateDate ASC";
                            break;
                        case "CreateBy":
                            dx.Tables[0].DefaultView.Sort = "CreateBy ASC";
                            break;
                    }
                    break;
                case "desc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description DESC";
                            break;
                        case "RGM":
                            dx.Tables[0].DefaultView.Sort = "RGMID DESC";
                            break;
                        case "ORGID":
                            dx.Tables[0].DefaultView.Sort = "ORGID DESC";
                            break;
                        case "LastModifiedDate":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedDate DESC";
                            break;
                        case "LastModifiedBy":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedBy DESC";
                            break;
                        case "CreateDate":
                            dx.Tables[0].DefaultView.Sort = "CreateDate DESC";
                            break;
                        case "CreateBy":
                            dx.Tables[0].DefaultView.Sort = "CreateBy DESC";
                            break;
                    }
                    break;
            }

            DataTable dt = dx.Tables[0].DefaultView.ToTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = PageNumber; i < Datalength; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        Location p = new Location();
                        p.ID = Convert.ToInt16(dt.Rows[i]["ID"].ToString());
                        p.Description = dt.Rows[i]["Description"].ToString();
                        p.isActive = Convert.ToBoolean(dt.Rows[i]["isActive"].ToString());
                        p.isHeadOffice = Convert.ToBoolean(dt.Rows[i]["isHeadOffice"].ToString());
                        p.RGM = new RGMController().GetRGMByID(Convert.ToInt16(dt.Rows[i]["RGMID"].ToString()));
                        p.LastModifiedBy = dt.Rows[i]["LastModifiedBy"].ToString();
                        p.LastModifiedDate = dt.Rows[i]["LastModifiedDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["LastModifiedDate"].ToString());
                        p.CreateBy = dt.Rows[i]["CreateBy"].ToString();
                        p.CreateDate = dt.Rows[i]["CreateDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["CreateDate"].ToString());
                        p.OrgID = dt.Rows[i]["OrgID"].ToString();
                        Locations.Add(p);
                    }
                    else
                    {
                        break;
                    }

                }
            }

            TotalData = dt.Rows.Count;

            return Locations.ToList();
        }


        public IList<Location> GetAllLocationsByRGMID(string sSearch, int RGMID, int PageNumber, int Datalength, string SortCol, string SortDirection, out int TotalData)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetAllLocationsByRGMID";
            command.Parameters.Add("@SEARCH", SqlDbType.VarChar).Value = sSearch;
            command.Parameters.Add("@RGMID", SqlDbType.VarChar).Value = RGMID;

            DataSet dx = GetTable(command);

            List<Location> Locations = new List<Location>();

            DataTable dt = dx.Tables[0].DefaultView.ToTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = PageNumber; i < Datalength; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        Location p = new Location();
                        p.ID = Convert.ToInt16(dt.Rows[i]["ID"].ToString());
                        p.Description = dt.Rows[i]["Description"].ToString();
                        p.isActive = Convert.ToBoolean(dt.Rows[i]["isActive"].ToString());
                        p.isHeadOffice = Convert.ToBoolean(dt.Rows[i]["isHeadOffice"].ToString());
                        p.RGM = new RGMController().GetRGMByID(Convert.ToInt16(dt.Rows[i]["RGMID"].ToString()));
                        p.LastModifiedBy = dt.Rows[i]["LastModifiedBy"].ToString();
                        p.LastModifiedDate = dt.Rows[i]["LastModifiedDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["LastModifiedDate"].ToString());
                        p.CreateBy = dt.Rows[i]["CreateBy"].ToString();
                        p.CreateDate = dt.Rows[i]["CreateDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["CreateDate"].ToString());
                        p.OrgID = dt.Rows[i]["OrgID"].ToString();
                        Locations.Add(p);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            TotalData = dt.Rows.Count;

            return Locations.ToList();
        }


        public Location GetLocationByID(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetLocationByID";
            command.Parameters.Add("@ID", SqlDbType.Int).Value = ID;

            IList<string> Data = GetSingleRow(command);
            Location Locations = null;

            if (Data != null && Data.Count > 0)
            {
                Locations = new Location();
                Locations.ID = Convert.ToInt16(Data[0]);
                Locations.Description = Data[1].ToString();
                Locations.isActive = Convert.ToBoolean(Data[2]);
                Locations.isHeadOffice = Convert.ToBoolean(Data[3]);
                Locations.RGM = new RGMController().GetRGMByID(Convert.ToInt16(Data[4]));
                Locations.LastModifiedDate = Data[5] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[5].ToString());
                Locations.LastModifiedBy = Data[6].ToString();
                Locations.CreateDate = Data[7] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[7].ToString());
                Locations.CreateBy = Data[8].ToString();
                Locations.OrgID = Data[9].ToString();
            }

            return Locations;
        }

        public Location GetLocationByDescription(string LocationDescription)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetLocationByDescription";
            command.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = LocationDescription;

            IList<string> Data = GetSingleRow(command);
            Location Locations = null;

            if (Data != null && Data.Count > 0)
            {
                Locations = new Location();
                Locations.ID = Convert.ToInt16(Data[0]);
                Locations.Description = Data[1].ToString();
                Locations.isActive = Convert.ToBoolean(Data[2]);
                Locations.isHeadOffice = Convert.ToBoolean(Data[3]);
                Locations.RGM = new RGMController().GetRGMByID(Convert.ToInt16(Data[4]));
                Locations.LastModifiedDate = Data[5] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[5].ToString());
                Locations.LastModifiedBy = Data[6].ToString();
                Locations.CreateDate = Data[7] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[7].ToString());
                Locations.CreateBy = Data[8].ToString();
                Locations.OrgID = Data[9].ToString();
            }

            return Locations;
        }
    }
}